import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: JSON.parse(localStorage.getItem('user')) || null,
    adminUser: {
      username: 'admin',
      password: '123456',
      isAdmin: true
    },
    cartItems: [],
    orders: [],
    drinks: [
      // 白酒
      {
        id: 101,
        name: '茅台飞天',
        price: 2899,
        category: '白酒',
        volume: '500ml',
        description: '酱香型白酒的代表，醇厚绵柔',
        image: require('@/assets/飞天.jpg')
      },
      {
        id: 102,
        name: '五粮液',
        price: 1299,
        category: '白酒',
        volume: '500ml',
        description: '浓香型白酒，香气馥郁',
        image: require('@/assets/五粮液.jpg')
      },
      // 红酒
      {
        id: 103,
        name: '拉菲古堡',
        price: 3999,
        category: '红酒',
        volume: '750ml',
        description: '法国波尔多顶级红酒，果香浓郁',
        image: require('@/assets/拉菲.jpg')
      },
      {
        id: 104,
        name: '白酒',
        price: 188,
        category: '黑酒',
        volume: '750ml',
        description: '中国经典黑酒，口感圆润',
        image: require('@/assets/白酒.jpg')
      },
      // 啤酒
      {
        id: 105,
        name: '青岛啤酒',
        price: 6,
        category: '啤酒',
        volume: '330ml',
        description: '清爽怡人，泡沫细腻持久',
        image: require('@/assets/青岛啤酒.jpg')
      },
      {
        id: 106,
        name: '百威啤酒',
        price: 8,
        category: '啤酒',
        volume: '330ml',
        description: '世界经典，口感醇正',
        image: require('@/assets/百威.jpg')
      },
      // 饮料
      {
        id: 107,
        name: '可口可乐',
        price: 4,
        category: '饮料',
        volume: '330ml',
        description: '经典汽水，清凉解暑',
        image: require('@/assets/可口.jpg')
      },
      {
        id: 108,
        name: '雪碧',
        price: 4,
        category: '饮料',
        volume: '330ml',
        description: '柠檬味汽水，清新爽口',
        image: require('@/assets/雪碧.jpg')
      }
    ],
    dishes: [
      // 川菜
      {
        id: 1,
        name: '宫保鸡丁',
        price: 38,
        category: '川菜',
        description: '经典川菜，鸡肉嫩滑，花生酥脆',
        image: require('@/assets/宫保.jpg')
      },
      {
        id: 2,
        name: '麻婆豆腐',
        price: 32,
        category: '川菜',
        description: '香辣可口，豆腐滑嫩',
        image: require('@/assets/麻婆豆腐.jpg')
      },
      {
        id: 3,
        name: '水煮鱼',
        price: 68,
        category: '川菜',
        description: '鱼肉鲜嫩，麻辣鲜香',
        image: require('@/assets/水煮鱼.jpg')
      },
      // 粤菜
      {
        id: 4,
        name: '白切鸡',
        price: 48,
        category: '粤菜',
        description: '鸡肉嫩滑，配姜葱酱',
        image: require('@/assets/白斩鸡.jpg')
      },
      {
        id: 5,
        name: '蜜汁叉烧',
        price: 56,
        category: '粤菜',
        description: '甜香可口，肉质鲜嫩',
        image: require('@/assets/叉烧.jpg')
      },
      // 江浙菜
      {
        id: 6,
        name: '东坡肉',
        price: 68,
        category: '江浙菜',
        description: '肥而不腻，入口即化',
        image: require('@/assets/东坡.jpg')
      },
      {
        id: 7,
        name: '龙井虾仁',
        price: 72,
        category: '江浙菜',
        description: '虾仁鲜嫩，茶香四溢',
        image: require('@/assets/虾仁.jpg')
      },
        { id: 12,
          name: '盱眙小龙虾',
          price: 108,
          category: '江浙菜',
          description: '甜香可口，肉质鲜嫩',
          image: require('@/assets/盱眙小龙虾.jpg')
        },
      // 湘菜
      {
        id: 8,
        name: '剁椒鱼头',
        price: 88,
        category: '湘菜',
        description: '鱼头新鲜，剁椒入味',
        image: require('@/assets/鱼头.jpg')
      },
      {
        id: 9,
        name: '农家小炒肉',
        price: 42,
        category: '湘菜',
        description: '香辣可口，下饭首选',
        image: require('@/assets/小炒肉.jpg')
      },
      // 特色小吃
      {
        id: 10,
        name: '蟹黄小笼包',
        price: 38,
        category: '特色小吃',
        description: '皮薄馅多，蟹香浓郁',
        image: require('@/assets/蟹黄.jpg')
      },
      {
        id: 11,
        name: '锅贴',
        price: 28,
        category: '特色小吃',
        description: '外酥内嫩，美味可口',
        image: require('@/assets/锅贴.jpg')
      }
    ],
    recommendDishes: {
      hotDishes: [
        {
          id: 1,
          name: '宫保鸡丁',
          price: 38,
          category: '川菜',
          description: '经典川菜，鸡肉嫩滑，花生酥脆',
          image: require('@/assets/宫保.jpg')
        },
        {
          id: 8,
          name: '剁椒鱼头',
          price: 88,
          category: '湘菜',
          description: '鱼头新鲜，剁椒入味',
          image: require('@/assets/鱼头.jpg')
        },
        {
        id: 2,
        name: '麻婆豆腐',
        price: 32,
        category: '川菜',
        description: '香辣可口，豆腐滑嫩',
        image: require('@/assets/麻婆豆腐.jpg')
      },
      {
        id: 3,
        name: '水煮鱼',
        price: 68,
        category: '川菜',
        description: '鱼肉鲜嫩，麻辣鲜香',
        image: require('@/assets/水煮鱼.jpg')
      },
      ],
      newDishes: [
        {
          id: 6,
          name: '东坡肉',
          price: 68,
          category: '江浙菜',
          description: '肥而不腻，入口即化',
          image: require('@/assets/东坡.jpg')
        },
        {
          id: 10,
          name: '蟹黄小笼包',
          price: 38,
          category: '特色小吃',
          description: '皮薄馅多，蟹香浓郁',
          image: require('@/assets/蟹黄.jpg')
        },
        {
        id: 4,
        name: '白切鸡',
        price: 48,
        category: '粤菜',
        description: '鸡肉嫩滑，配姜葱酱',
        image: require('@/assets/白斩鸡.jpg')
      },
       { id: 12,
          name: '盱眙小龙虾',
          price: 108,
          category: '江浙菜',
          description: '甜香可口，肉质鲜嫩',
          image: require('@/assets/盱眙小龙虾.jpg')
        }
      ],
      chefDishes: [
        {
          id: 7,
          name: '龙井虾仁',
          price: 72,
          category: '江浙菜',
          description: '虾仁鲜嫩，茶香四溢',
          image: require('@/assets/虾仁.jpg')
        },
        {
          id: 5,
          name: '蜜汁叉烧',
          price: 56,
          category: '粤菜',
          description: '甜香可口，肉质鲜嫩',
          image: require('@/assets/叉烧.jpg')
        },
        {
        id: 11,
        name: '锅贴',
        price: 28,
        category: '特色小吃',
        description: '外酥内嫩，美味可口',
        image: require('@/assets/锅贴.jpg')
      },
       {
        id: 9,
        name: '农家小炒肉',
        price: 42,
        category: '湘菜',
        description: '香辣可口，下饭首选',
        image: require('@/assets/小炒肉.jpg')
      }
      ]
    }
  },
  mutations: {
    setUser(state, user) {
      state.user = user
      if (user) {
        localStorage.setItem('user', JSON.stringify(user))
      } else {
        localStorage.removeItem('user')
      }
    },
    addToCart(state, dish) {
      const existItem = state.cartItems.find(item => item.id === dish.id)
      if (existItem) {
        existItem.quantity++
      } else {
        state.cartItems.push({...dish, quantity: 1})
      }
    },
    removeFromCart(state, dishId) {
      state.cartItems = state.cartItems.filter(item => item.id !== dishId)
    },
    updateQuantity(state, {dishId, quantity}) {
      const item = state.cartItems.find(item => item.id === dishId)
      if (item) {
        item.quantity = quantity
      }
    },
    clearCart(state) {
      const newOrder = {
        id: Date.now(),
        items: [...state.cartItems],
        total: state.cartItems.reduce((sum, item) => sum + item.price * item.quantity, 0),
        date: new Date().toLocaleString(),
        status: '已完'
      }
      state.orders.unshift(newOrder)
      state.cartItems = []
    },
    addDish(state, dish) {
      state.dishes.push(dish)
    },
    updateDish(state, updatedDish) {
      const index = state.dishes.findIndex(dish => dish.id === updatedDish.id)
      if (index !== -1) {
        state.dishes.splice(index, 1, updatedDish)
      }
    },
    deleteDish(state, dishId) {
      state.dishes = state.dishes.filter(dish => dish.id !== dishId)
    },
    addDrink(state, drink) {
      state.drinks.push(drink)
    },
    updateDrink(state, updatedDrink) {
      const index = state.drinks.findIndex(drink => drink.id === updatedDrink.id)
      if (index !== -1) {
        state.drinks.splice(index, 1, updatedDrink)
      }
    },
    deleteDrink(state, drinkId) {
      state.drinks = state.drinks.filter(drink => drink.id !== drinkId)
    },
    addRecommend(state, { type, dish }) {
      if (state.recommendDishes[type]) {
        state.recommendDishes[type].push(dish)
      }
    },
    removeRecommend(state, { type, index }) {
      if (state.recommendDishes[type]) {
        state.recommendDishes[type].splice(index, 1)
      }
    }
  },
  getters: {
    cartTotal: state => {
      return state.cartItems.reduce((total, item) => total + item.price * item.quantity, 0)
    },
    isAuthenticated: state => {
      return !!state.user
    },
    isAdmin: state => {
      return state.user && state.user.isAdmin
    }
  }
}) 